/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author Guilherme
 */
import java.sql.*;
import java.util.ArrayList;

public class Link
{
    public static Connection conecta()
    {
        String url = "jdbc:postgresql://galileo.facom.ufu.br:5432/sbd3";
        String username = "bcc3";
        String password = "bcc";
        String url2 = "jdbc:postgresql://localhost:5432/sbd3";
        String username2 = "postgres";
        String password2 = "flop123";
        Connection connection = null;

        try
        {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection(url2, username2, password2);
            String str = "SET search_path TO sib";
            int rs = connection.createStatement().executeUpdate(str);
        } catch (ClassNotFoundException cnfex)
        {
            System.err.println("Failed to load JDBC/ODBC driver. erro");
            cnfex.printStackTrace();
            return null;
        } catch (SQLException sqlex)
        {
            System.err.println("Unable to connect");
            sqlex.printStackTrace();
            return null;
        }
        return connection;
    }
    public static boolean add (String tableName, String values)
    {
        try 
        { 
            Statement stm = Link.conecta().createStatement(); 
            String str = "INSERT INTO "+tableName+" VALUES "+values+";";
            int rs = stm.executeUpdate(str);
            return true;
        } 
        catch (SQLException e)
        {
            return false;
        } 
    }
    public static boolean addX (ArrayList<String> tabelas, ArrayList<String> valores)
    {
        String str = "BEGIN TRANSACTION;\n";
        for (int i = 0; i < tabelas.size(); i++)
        {
            str = str + "INSERT INTO " + tabelas.get(i) + " VALUES " + valores.get(i) + "\n";         
        }
        str = str + "END TRANSACTION;";
        try 
        { 
            Statement stm = Link.conecta().createStatement(); 
            int rs = stm.executeUpdate(str);
        } 
        catch (SQLException e)
        {
            return false;
        }
        return true;
    }
    public static boolean delete (String tableName, String where)
    {
        try 
        { 
            Statement stm = Link.conecta().createStatement(); 
            String str = "DELETE FROM "+tableName+" WHERE "+where+";";
            int rs = stm.executeUpdate(str);

            return true;
        } 
        catch (SQLException e)
        {
            System.out.println ("ERRO: Falha ao tentar deletar o " + tableName + ": " + where);
            return false;
        } 
    }
    
    public static boolean update (String tableName, String set, String where)
    {
        try 
        { 
            Statement stm = Link.conecta().createStatement(); 
            int rs = stm.executeUpdate("UPDATE "+tableName+ " SET " + set + " WHERE "+ where +";");

            return true;
        } 
        catch (SQLException e)
        {
            return false;
        } 
    }
    
    public static ResultSet select (String tableName, String select, String where)
    {
        try 
        {
            Statement stm = Link.conecta().createStatement();
            String s = "SELECT "+select+" FROM "+tableName+" WHERE "+where;
            return stm.executeQuery(s);
        } 
        catch (SQLException e) 
        {
            return null;
        }
    }
}